<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生命周期</title>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">

        class App extends React.Component{
            state = {
                keyword: 'game'  
            }

            render(){
                return <div className="container">
                        <button k="city" onClick={this.change}>城市</button>
                        <button k="animal" onClick={this.change}>动物</button>
                        <button k="car" onClick={this.change}>汽车</button>
                        <hr />
                        <Image a="100" src={'https://source.unsplash.com/500x400/?' + this.state.keyword} />
                    </div>
            }

            change = (e) => {
                this.setState({
                    keyword: e.target.getAttribute('k') 
                })
            }
        }

        class Image extends React.Component{
            render(){
                console.log('更新中...渲染中...');
                return <img src={this.props.src} alt="" />
            }

            //组件将要接收 props 数据
            componentWillReceiveProps(){
                console.log('组件将要接收 props 数据')
            }

            //
            shouldComponentUpdate(){
                console.log('组件应该要更新');
                return true;
            }

            //
            componentWillUpdate(){
                console.log('组件将要更新')
            }

            //
            componentDidUpdate(){
                console.log('完成更新');
            }

        }

        ReactDOM.render(<App />, document.querySelector("#root"));

    </script>
</body>
</html>